package cn.edu.ctbu.teachers.api;


import cn.edu.ctbu.teachers.constant.REnum;
import cn.edu.ctbu.teachers.entity.Teacher;
import cn.edu.ctbu.teachers.exception.RException;
import cn.edu.ctbu.teachers.service.TeacherService;
import cn.edu.ctbu.teachers.util.RUtil;
import cn.edu.ctbu.teachers.vo.QueryObj;
import cn.edu.ctbu.teachers.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@RequestMapping("/api/teacher")
@Slf4j
public class TeacherApiController {
    @Autowired
    private  final TeacherService teacherService;
    public TeacherApiController(TeacherService teacherService) {
        this.teacherService = teacherService;
    }

    @GetMapping("/list")
    public R<List<Teacher>> findAll() {
        List<Teacher> teachers=teacherService.findALL();
        return RUtil.success(teachers);
    }

    @GetMapping("/{id}")
    public R<Teacher> findById(@PathVariable int id) {
        Teacher teacher=teacherService.findById(id);
        return RUtil.success(teacher);
    }

    @PostMapping("/add")
    public R<Teacher> add(Teacher teacher) {

        return RUtil.success(teacherService.insert(teacher));
    }

    @PutMapping("/update")
    public R<Teacher> update(Teacher teacher) {

        return RUtil.success(teacherService.update(teacher));
    }

    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Integer id) {

            teacherService.delete(id);
        return RUtil.success();
    }

    @DeleteMapping("/deleteId/{ids}")
    public R deleteByIds(@PathVariable String ids) {
        try {
            List<Integer> teachIds = Stream.of(ids.split(","))
                    .map(Integer::parseInt).collect(Collectors.toList());
            teacherService.deleteByIds(teachIds);
        } catch (Exception e) {
            return RUtil.error(1, e.getMessage());
        }
        return RUtil.success();
    }


    @GetMapping("/validateUser")
    public R validateTnoAndPassword(String tno,String password) throws Exception{
        return RUtil.success(teacherService.validateUsernameAndPassword(tno, password));
    }

    @PostMapping("/getbypage")
    public R<Page<Teacher>> getByPage(@RequestBody QueryObj<Teacher> qObj){
        //按id排倒序
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Integer pageIndex = 0;
        Integer pageSize = 10;

        if(qObj==null){
            //teacher为空就直接调用分页
            Pageable pageable = PageRequest.of(pageIndex,pageSize,sort);
            Page<Teacher> teachers =teacherService.getAll(pageable);
            return RUtil.success(teachers.getContent(),teachers.getTotalElements());
        }else {
            pageIndex= qObj.getPage()-1;
            pageSize= qObj.getLimit();

            if(qObj.getData() instanceof Teacher){
                Teacher teacher=(Teacher)qObj.getData();
                Pageable pageable = PageRequest.of(pageIndex,pageSize,sort);

                ExampleMatcher matcher=ExampleMatcher.matching()
                        .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains())
                        .withIgnoreNullValues();//忽略空值

                Example<Teacher> example=Example.of(teacher,matcher);
                Page<Teacher> teacherPage =teacherService.getAll(example,pageable);
                return RUtil.success(teacherPage.getContent(),teacherPage.getTotalElements());
            }else {
                throw new RException(REnum.QUERY_ERR);
            }
        }
    }


}
